package com.expense.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesReader {
    private static PropertiesReader instance = null;

    private Properties properties = null;

   
    public static PropertiesReader getIntance() {
        if (instance == null)
            instance = new PropertiesReader("/conf.properties");
        return instance;
    }

   
    public static PropertiesReader getIntance(String path) {
        instance = new PropertiesReader(path);
        return instance;
    }

    
    private PropertiesReader(String path) {
        properties = this.createProperties(path);
        if (properties == null)
            properties = new Properties();
    }

   
    public Integer getInt(String key) {
        String prop = this.getProperty(key);
        if (prop.equals(""))
            return new Integer(0);
        return Integer.valueOf(prop);
    }

    public Long getLong(String key) {
        String prop = this.getProperty(key);
        if (prop.equals(""))
            return new Long(0);
        return Long.valueOf(prop);
    }

    public String getProperty(String key) {
        if (StringUtils.chgNull(key).equals(""))
            return "";
        return StringUtils.chgNull(properties.getProperty(key));
    }

   
    public String getProperty(String key, String defaultValue) {
        if (StringUtils.chgNull(key).equals(""))
            return "";
        return StringUtils.chgNull(properties.getProperty(key, defaultValue));
    }

   
    public String getConvertGBK(String key) {
        String rs = "";
        String prop = this.getProperty(key);
        if (prop.equals(""))
            return rs;
        else {
            try {
                rs = new String(prop.getBytes("ISO8859_1"), "GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return rs;
    }

    public Enumeration getValues() {
        try {
            return properties.elements();
        } catch (Exception ex) {
            return null;
        }
    }

  
    public Enumeration getKeys() {
        try {
            return properties.propertyNames();
        } catch (Exception ex) {
            return null;
        }
    }

    private Properties createProperties(String path) {
        Properties p = null;
        Object obj = null;
        try {
            obj = getClass().getResourceAsStream(path);
            if (obj == null)
                obj = new FileInputStream(path);
        } catch (IOException e) {
            System.err.println("System can not find the property!");
        }
        if (obj != null) {
            try {
                p = new Properties();
				p.load((InputStream) obj);
				((InputStream) obj).close();
			} catch (IOException e) {
				p = null;
			}
		}
		return p;
	}
	
}

